package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.validate.ValidGroupAdd;
import com.hoshiicloud.common.validate.ValidGroupUpdate;
import com.hoshiicloud.user.entity.company.ComDepartments;
import com.hoshiicloud.user.param.company.ComDepartmentsQueryParam;
import com.hoshiicloud.user.service.company.IComDepartmentsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Slf4j
@RestController
@RequestMapping("/company/department")
@Api(value = "SAAS租户部门", tags = {"SAAS租户部门"})
@ApiSort(8)
public class ComDepartmentsController {

    @Autowired
    private IComDepartmentsService departmentsService;

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询部门信息
     */
    @GetMapping("/{id}")
    @ApiOperation(
            value = "查询",
            notes = "查询部门信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response findDeptById(@PathVariable("id")  Long id) {
        return Response.data(departmentsService.getById(id));
    }

    /**
     * @param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "查询",
            notes = "根据名称查询",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response findDeptByName(String name) {

        Wrapper wrapper = Wrappers.<ComDepartments>query().lambda().eq(ComDepartments::getName, name);

        return Response.data(departmentsService.getOne(wrapper));

    }

    /**
     * @param departments
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增",
            notes = "新增部门信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response saveDept(@RequestBody @Validated(value = ValidGroupAdd.class) ComDepartments departments) {

        return Response.data(departmentsService.save(departments));
    }

    /**
     * @param departments
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改",
            notes = "修改部门信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response updateDeptById(@RequestBody @Validated(value = ValidGroupUpdate.class) ComDepartments departments) {
        return Response.data(departmentsService.updateById(departments));

    }

    @PostMapping("/getPage")
    public Response getPage(@RequestBody @Valid ComDepartmentsQueryParam param){
        try {
            QueryWrapper<ComDepartments> queryWrapper=new QueryWrapper();
            queryWrapper.lambda()
                    .eq(param.getCommpanyId()!=null,ComDepartments::getCommpanyId,param.getCommpanyId())
                    .eq(param.getParentId()!=null,ComDepartments::getParentId,param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), ComDepartments::getName, param.getName());
            Page<ComDepartments> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(departmentsService.getPage(page,queryWrapper,key));
        }catch (Exception e){
            log.error("分页查询租户供应商部门异常", e);
            return Response.failed("分页查询租户供应商部门异常");
        }
    }
}
